package cn.bdqn.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.bdqn.pojo.Provider;
import cn.bdqn.pojo.User;
import cn.bdqn.service.ProviderService;
import cn.bdqn.util.PageBean;

@Controller
@RequestMapping("/provider")
public class ProviderController {
	
	@Autowired
	private ProviderService providerService;
	
	@RequestMapping("/showPage")
	public String showPage(@RequestParam(value="queryProName",required=false)String proName,
			@RequestParam(value="queryProCode",required=false)String proCode,
			@RequestParam(value="pageNo",required=false,defaultValue="1")int pageNo,
			Model model){
		int pageSize=6;
		PageBean<Provider> pageBean=providerService.findByPage(pageNo, pageSize, proName, proCode);
		model.addAttribute("queryProName",proName);
		model.addAttribute("queryProCode",proCode);
		model.addAttribute("pageBean",pageBean);
		return "providerlist";
	}
	
	@ResponseBody
	@RequestMapping("/proview/{proCode}")
	public Object view(@PathVariable String proCode){
		Provider provider=providerService.findByCode(proCode);
		return provider;
	}
	
	@RequestMapping("/promodify/{proCode}")
	public String promodify(@PathVariable String proCode,Model model){
		Provider provider=providerService.findByCode(proCode);
		model.addAttribute("provider",provider);
		return "providermodify";
	}
	
	@RequestMapping("/prodomodify")
	public String prodomodify(Provider provider,HttpSession session){
		provider.setModifyBy(((User)session.getAttribute("userlogin")).getId());
		provider.setModifyDate(new Date());
		boolean isOk=providerService.updateByProvider(provider);
		if(isOk){
			return "prosuccess";
		}else{
			return "providermodify";
		}
	}
	
	@RequestMapping("/provideradd.html")
	public String showProvideradd(){
		return "provideradd";
	}
	
	@RequestMapping("/proadd.do")
	public String provideradd(Provider provider,HttpSession session){
		provider.setCreatedBy(((User)session.getAttribute("userlogin")).getId());
		provider.setCreationDate(new Date());
		boolean isOk=providerService.addProvider(provider);
		if(isOk){
			return "prosuccess";
		}else{
			return "provideradd";
		}		
	}
	
	@ResponseBody
	@RequestMapping(value="/checkProCode")
	public Object checkUserCode(@RequestParam("proCode")String proCode){
		boolean isOk=providerService.checkProCode(proCode);
		Map<String, Object> map=new HashMap<String, Object>();
		if(isOk){
			map.put("proCode","exist");
		}else{
			map.put("proCode","noexist");
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/delete/{proCode}")
	public Object delete(@PathVariable("proCode")String proCode,@RequestParam("proid")Integer id){
		Map<String, Object> map=new HashMap<String, Object>();
		if(providerService.findByCode(proCode)!=null){
			Map<String, Object> obj=(Map<String, Object>) providerService.deleteById(id);
			int result=(Integer) obj.get("result");
			if(result>0){
				map.put("delResult", "true");
			}else{
				map.put("delResult", "false");
			}
			int billSum=(Integer) obj.get("billSum");
			if(billSum!=0){
				map.put("delResult", billSum);
			}
		}else{
			map.put("delResult","notexist");
		}
		return map;
	}
}
